<?php
/**
 * 钱包控制器
 * Created by PhpStorm.
 * User: sk
 * Date: 2018/5/29
 * Time: 17:14
 */

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WalletController extends BaseController
{
    /**
     * [钱包个人余额api]
     * @Author   sk
     * @DateTime 2018-05-24
     * @version  0.8
     * @return array
     */
    public function balance(Request $request)
    {
        $arr = (JWTAuth::parseToken()->authenticate());
        $data = json_decode($arr, true);

        /**
         *  type==1为个人，2 企业个人(员工,部门主管) 3管理员
         * $sum_usd=个人余额+企业余额-未完成的订单金额
         * 查询未完成订单金额 $status!=9
         */
        $uid = $data['id'] ? $data['id'] : '';
        $where['user_id'] = $uid;
        $where['wystatus'] = 1;
        $company_usd = Db::table('order')->where($where)->pluck('price')->toarray();
        $count = array_sum($company_usd);//所有未结算订单的金额的总数
        $sum_usd = $data['type'] !== 1 ? $data['left_price'] + $data['sum_money'] - $count : $data['sum_money'] - $count;
        $money = json_decode(json_encode($sum_usd), true);
        /**
         * $count 进行中的占用的额度
         * $money 余额
         * $data['card_money'] 出行卡余额
         * $data['plane_left_num'] + $data['p_plane_left_num']; 企业分配接送机剩余次数+个人购买接送机剩余次数
         */
        $all['count']=$count;
        $all['sum_money'] = $money;//余额
        $all['card_money'] = $data['card_money'];
        $all['number'] = $data['plane_left_num'] + $data['p_plane_left_num'];
        $sum_money = json_encode($all);
        if (!empty($all)) {
            return response(['all' => $sum_money, 'code' => 1000, 'message' => '余额查询成功！'],200);
        } else {
            return response(['code' => 1001, 'message' => '余额查询失败，请从试一次！'],400) ;
        }

    }


}